package com.ebt.m.customer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.customer.CustomerUtil;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.entity.CustomerDetail;
import com.ebt.m.customer.event.EventCustomerAllPageIndex;
import com.ebt.m.customer.event.EventCustomerRefresh;
import com.ebt.m.customer.net.json.CustomerDetailJson;
import com.ebt.m.customer.ui.ActivityCustomerAll;
import com.ebt.m.data.entity.Customer;
import com.ebt.m.data.rxModel.api.EBTAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * Created by Rick.Wang on 2016/4/29.
 */
public class CustomerAllHeaderView extends FrameLayout implements View.OnClickListener {

    //view
    private ViewGroup mContent;
    private TextView mName;
    private TextView mTagView;
    private ImageView mAvatarView;

    //data
    private CustomerDetail mCustomerDetail;

    public CustomerAllHeaderView(Context context) {
        super(context);
    }

    public CustomerAllHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    private void init(Context context) {
        mContent = (ViewGroup) inflate(context, R.layout.view_customer_all_header, this);
        mTagView = (TextView) mContent.findViewById(R.id.tv_tag);
        mName = (TextView) mContent.findViewById(R.id.name);
        mAvatarView = (ImageView) mContent.findViewById(R.id.avatar);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tag:
                EventBus.getDefault().post(new EventCustomerAllPageIndex(CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_INSURANCE));
                break;
        }
    }

    public View getAvatarView() {
        return mAvatarView;
    }

    public View getTextViewName() {
        return mName;
    }

    public void update(Customer customer) {
        updateTextViews(customer);
        updateAvatarView(customer);
    }

    public void update(CustomerDetail customerDetail) {
        mCustomerDetail = customerDetail;
        if (customerDetail != null) {
            updateTextViews(mCustomerDetail);
            updateAvatarView(mCustomerDetail);
            updateTagView(mCustomerDetail.getTag());
        }
    }

    private void updateTextViews(Customer customer) {
        mName.setText(customer.getName());
        updateTagView(customer.getTag());
    }

    private void updateTextViews(CustomerDetail customerDetail) {
        mName.setText(customerDetail.getName());
        updateTagView(customerDetail.getTag());
    }

    private void updateTagView(Integer tag) {
        mTagView.setText(CustomerConstant.CustomerTag.getShowText(tag));
        mTagView.setOnClickListener(this);
        mTagView.setVisibility(VISIBLE);
    }

    private void updateAvatarView(CustomerDetail customerDetail) {
        Customer customer = new Customer();
        customer.setSex(customerDetail.getSex());
        customer.setPortraitPath(customerDetail.getPortraitPath());
        updateAvatarView(customer);
    }

    private void updateAvatarView(Customer customer) {
        String url = null;
        Integer sex = null;
        if (customer != null) {
            url = customer.getPortraitPath();
            sex = customer.getSex();
        }
        CustomerUtil.setAvatarByUrlAndSex(getContext(), mAvatarView, url, sex);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCustomerRefresh event) {
        if (event != null) {
            EBTAPI ebtapi = AppInitService.getEbtApi();
            ebtapi.getCustomerDetail(event.customerId).compose(RxUtil.rxTransformer((ActivityCustomerAll) getContext())).subscribe(getCustomerDetailSubscriber());
        }
    }

    private Observer<CustomerDetailJson> getCustomerDetailSubscriber() {
        return new Observer<CustomerDetailJson>() {

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(CustomerDetailJson customerDetailJson) {
                if (customerDetailJson.error == null && customerDetailJson.data != null) {
                    updateTextViews(customerDetailJson.data);
                    updateAvatarView(customerDetailJson.data);
                }
            }
        };
    }
}